package com.music.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.dao.*;
import com.music.entity.*;
import com.music.service.UserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Classname UserLikeServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/7/4 16:29
 * @Created by Administrator
 */
@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private UserLikeMapper userLikeMapper;

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private SingerMapper singerMapper;

    @Autowired UserMapper userMapper;

    @Autowired
    private SongListMapper songListMapper;

    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private MvMapper mvMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public PageInfo getUserLikeByCondition(Integer pageNum, Integer pageSize, Integer typeId, String userId) {


        UserLike userLike = new UserLike();
        userLike.setUserId(userId);
        userLike.setUserLikeType(typeId);
        PageHelper.startPage(pageNum,pageSize);
        List<UserLike> userLikeList = userLikeMapper.selectBycondition(userLike);
        List list = new ArrayList();
        if(userLikeList != null){
            /* !!!!!!!!! 下次不要for循环查询数据库 where in(*,*,*,*) */
            userLikeList.forEach(userLike1 -> {
                if(typeId == 0){
                    // 设置收藏的歌曲列表
                    Song song = songMapper.selectByPrimaryKey(userLike1.getUserLikeObjId());
                    song.setSinger(singerMapper.selectByPrimaryKey(song.getSingerId()));
                    song.setAlbum(albumMapper.selectByPrimaryKey(song.getAlbumId()));
                    list.add(song);
                }else if(typeId == 1){
                    // 设置收藏的歌单列表
                    SongList songList =songListMapper.selectByPrimaryKey(userLike1.getUserLikeObjId()).get(0);
                    songList.setSongNum(songListMapper.countByCondition(songList));
                    songList.setUser(userMapper.findByUserId(userLike1.getUserId()));
                    list.add(songList);
                }else if(typeId == 2){
                    // 设置收藏的专辑列表
                    Album album = albumMapper.selectByPrimaryKey(userLike1.getUserLikeObjId());
                    album.setSongNum(albumMapper.countByCondition(album));
                    album.setSinger(singerMapper.selectByPrimaryKey(album.getSingerId()));
                    list.add(album);
                }else{
                    // 设置收藏的MV列表
                    Mv mv = mvMapper.selectByPrimaryKey(userLike1.getUserLikeObjId());
                    mv.setSinger(singerMapper.selectByPrimaryKey(mv.getSingerId()));
                    list.add(mv);
                }
            });
        }
        PageInfo pageInfo = new PageInfo<>(list,5);
        return pageInfo;
    }

    @Override
    public List<UserLikeVo> countUserLike(String userId) {
        UserLike userLike = new UserLike();
        userLike.setUserId(userId);

        UserLikeVo[] userLikeVos = new UserLikeVo[4];
        for(int i = 0; i < userLikeVos.length; i++){
                userLikeVos[i] = new UserLikeVo();
        }
        userLikeVos[0].setObjName("歌曲");
        userLike.setUserLikeType(0);
        userLikeVos[0].setCount(userLikeMapper.countByCondition(userLike));

        userLikeVos[1].setObjName("歌单");
        userLike.setUserLikeType(1);
        userLikeVos[1].setCount(userLikeMapper.countByCondition(userLike));

        userLikeVos[2].setObjName("专辑");
        userLike.setUserLikeType(2);
        userLikeVos[2].setCount(userLikeMapper.countByCondition(userLike));

        userLikeVos[3].setObjName("视频");
        userLike.setUserLikeType(3);
        userLikeVos[3].setCount(userLikeMapper.countByCondition(userLike));

        return Arrays.asList(userLikeVos);
    }
}
